package adapter;

import android.app.Activity;
import android.graphics.Color;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.bumptech.glide.Glide;
import com.youth.banner.loader.ImageLoader;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import base.BaseObserver;
import bean.SchoolPostCommentListResultBean;
import de.hdodenhof.circleimageview.CircleImageView;
import io.reactivex.Observable;
import jiyun.com.yiyanmeng.R;
import jiyun.com.yiyanmeng.http.HttpManager;
import jiyun.com.yiyanmeng.http.MySerivce;
import utils.MyListView;
import utils.ProgressDialogs;
import utils.RxUtil;
import utils.ToastUtil;


public class CommentDetailAdapter extends RecyclerView.Adapter<CommentDetailAdapter.NyViewholder> {

    private Activity context;
    private List<SchoolPostCommentListResultBean.SchoolPostCommentBean> list=new ArrayList<>();
    private BottomSheetDialog dialog;
    private String add_ping;//添加评论

    public void setType(int type) {
        this.type = type;
        if (type==3)
        {
            add_ping="forumsc/add_ping";
        }else if (type==1)
        {
            add_ping="forum/add_ping";
        }else
        {
            add_ping="forumjy/add_ping";
        }
    }

    private int type;//1 官方论坛详情评论  2 经验论坛详情评论 3学校评论列表点赞

    public CommentDetailAdapter(Activity context, List<SchoolPostCommentListResultBean.SchoolPostCommentBean> list) {
        this.context = context;
        this.list = list;

    }

    @Override
    public NyViewholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_comment_detail, parent, false);
        return new NyViewholder(view);
    }

    @Override
    public void onBindViewHolder(final NyViewholder holder, final int position) {
        holder.adapter=new MyPostReplyBasedaapter( context);
        holder.adapter.setType(type);
        holder.adapter.setBean(list.get(position));
//        holder.adapter.setList(list1);
        holder.myListview.setAdapter(holder.adapter);

        //评论回复列表
        if(list.get(position).reply == null || list.get(position).reply.size() == 0){
            holder.myListview.setVisibility(View.GONE);
        }else{
            holder.myListview.setVisibility(View.VISIBLE);
        }

        //头像
        if (this.list.get(position).mUser!=null) {
            if (!TextUtils.isEmpty(this.list.get(position).mUser.img)) {
                Glide.with(context).load(list.get(position).mUser.img).into(holder.image);
            }
            if (!TextUtils.isEmpty(this.list.get(position).mUser.userName)) {
                holder.tv_userName.setText(this.list.get(position).mUser.userName);
            }
        }
        //用户名
        holder.tv_createTime.setText(this.list.get(position).createTime);
        holder.txt_text.setText(this.list.get(position).text);
        if (!TextUtils.isEmpty(list.get(position).pingNum))
        {
//            holder.tv_comment.setText(list.get(position).pingNum);
        }else
        {
            list.get(position).pingNum="0";
        }
        //点击回复
        holder.tv_reply.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showCommentDialog(list.get(position));
            }
        });
        //点赞
        holder.iv_zan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (list.get(position).is_zan.equals("1"))
                {
                    ToastUtil.show(context,"您已赞过");
                    return;
                }
                if (type==3)
                {
                    //3学校评论列表点赞
                    zan(list.get(position).commenId,position);
                }else if (type==1)
                {
                    //1 官方论坛详情评论
                    zanOfficial(list.get(position).commenId,position);
                }else
                {
                    //2 经验论坛详情评论
                    zanExperience(list.get(position).commenId,position);
                }

            }
        });

        if (!TextUtils.isEmpty(list.get(position).zanNum))
        {
            holder.tv_zan_num.setText(list.get(position).zanNum);
        }else
        {
            list.get(position).zanNum="0";
        }

        if (list.get(position).is_zan.equals("0"))
        {
            holder.iv_zan.setColorFilter(Color.parseColor("#aaaaaa"));
        }else
        {
            holder.iv_zan.setColorFilter(Color.parseColor("#FF5C5C"));
        }
    }

    private void zan(String id, final int pos) {
        ProgressDialogs.showProgressDialog(context);
        MySerivce serivce = HttpManager.getInstance().getSerivce(MySerivce.base_URL, MySerivce.class);
        Observable <SchoolPostCommentListResultBean> pinbean3 = serivce.pinbean3(id);
        pinbean3.compose(RxUtil.<SchoolPostCommentListResultBean>changeThread())
                .subscribe(new BaseObserver <SchoolPostCommentListResultBean>() {
                    @Override
                    public void onSuccess(SchoolPostCommentListResultBean bean) {
                        if (bean.ret.equals("200")){
                            ToastUtil.show(context,"点赞成功");
                            list.get(pos).is_zan="1";
                            list.get(pos).zanNum=String.valueOf(Integer.parseInt(list.get(pos).zanNum)+1);
                            notifyDataSetChanged();
                        }else{
                            ToastUtil.show(context,bean.mas);
                        }
                    }

                    @Override
                    public void onFail(String error) {

                    }
                });
    }

    private void zanOfficial(String id, final int pos) {
        ProgressDialogs.showProgressDialog(context);
        MySerivce serivce = HttpManager.getInstance().getSerivce(MySerivce.base_URL, MySerivce.class);
        Observable <SchoolPostCommentListResultBean> pinbean4 = serivce.pinbean4(id);
        pinbean4.compose(RxUtil.<SchoolPostCommentListResultBean>changeThread())
                .subscribe(new BaseObserver <SchoolPostCommentListResultBean>() {
                    @Override
                    public void onSuccess(SchoolPostCommentListResultBean bean) {
                        if (bean.ret.equals("200")){
                            ToastUtil.show(context,"点赞成功");
                            list.get(pos).is_zan="1";
                            list.get(pos).zanNum=String.valueOf(Integer.parseInt(list.get(pos).zanNum)+1);
                            notifyDataSetChanged();
                        }else{
                            ToastUtil.show(context,bean.mas);
                        }
                    }

                    @Override
                    public void onFail(String error) {

                    }
                });
    }

    private void zanExperience(String id, final int pos) {
        ProgressDialogs.showProgressDialog(context);
        MySerivce serivce = HttpManager.getInstance().getSerivce(MySerivce.base_URL, MySerivce.class);
        Observable <SchoolPostCommentListResultBean> pinbean5 = serivce.pinbean5(id);
        pinbean5.compose(RxUtil.<SchoolPostCommentListResultBean>changeThread())
                .subscribe(new BaseObserver <SchoolPostCommentListResultBean>() {
                    @Override
                    public void onSuccess(SchoolPostCommentListResultBean bean) {
                        if (bean.ret.equals("200")){
                            ToastUtil.show(context,"点赞成功");
                            list.get(pos).is_zan="1";
                            list.get(pos).zanNum=String.valueOf(Integer.parseInt(list.get(pos).zanNum)+1);
                            notifyDataSetChanged();
                        }else{
                            ToastUtil.show(context,bean.mas);
                        }
                    }

                    @Override
                    public void onFail(String error) {

                    }
                });
    }


    @Override
    public int getItemCount() {
        return list.size();
    }

    /**
     * func:弹出评论框
     * @param
     */
    private void showCommentDialog(final SchoolPostCommentListResultBean.SchoolPostCommentBean bean){
        dialog = new BottomSheetDialog(context);
        View commentView = LayoutInflater.from(context).inflate(R.layout.layout_comment_dialog,null);
        final EditText commentText = (EditText) commentView.findViewById(R.id.edit_content);
        final TextView bt_comment = (TextView) commentView.findViewById(R.id.tv_finish);
        dialog.setContentView(commentView);
        /**
         * 解决bsd显示不全的情况
         */
        View parent = (View) commentView.getParent();
        BottomSheetBehavior behavior = BottomSheetBehavior.from(parent);
        commentView.measure(0,0);
        behavior.setPeekHeight(commentView.getMeasuredHeight());

        bt_comment.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                String commentContent = commentText.getText().toString().trim();
                if(!TextUtils.isEmpty(commentContent)){
                    submit(bean,commentContent);
                }else {
                    Toast.makeText(context,"评论内容不能为空",Toast.LENGTH_SHORT).show();
                }
            }
        });
        dialog.show();
    }


    private void submit(SchoolPostCommentListResultBean.SchoolPostCommentBean bean,String info) {
        ProgressDialogs.showProgressDialog(context);
        MySerivce serivce = HttpManager.getInstance().getSerivce(MySerivce.base_URL+add_ping, MySerivce.class);
        serivce.pinbean6(bean.commenId,info).compose(RxUtil.<SchoolPostCommentListResultBean>changeThread())
                .subscribe(new BaseObserver <SchoolPostCommentListResultBean>() {
                    @Override
                    public void onSuccess(SchoolPostCommentListResultBean bean) {
                        if (bean.ret.equals("200")){
//                            EventBus.getDefault().post(""); //普通事件发布
                            notifyDataSetChanged();
                            dialog.dismiss();
                        }else{
                            ToastUtil.show(context,bean.mas);
                        }
                    }

                    @Override
                    public void onFail(String error) {

                    }
                });
    }

    public class NyViewholder extends RecyclerView.ViewHolder {
        ImageView image;
        TextView tv_userName,tv_createTime,txt_text,tv_zan_num,tv_reply;
        RelativeLayout rl_address;
        MyListView myListview;
        MyPostReplyBasedaapter adapter;
        ImageView iv_zan;

        public NyViewholder(View itemView) {
            super(itemView);
            myListview=itemView.findViewById(R.id.myListview);
            tv_userName = itemView.findViewById(R.id.tv_userName);
            tv_createTime = itemView.findViewById(R.id.tv_createTime);
            txt_text =  itemView.findViewById(R.id.txt_text);
            tv_reply =  itemView.findViewById(R.id.tv_reply);
            image=itemView.findViewById(R.id.image);
//            tv_comment=itemView.findViewById(R.id.tv_comment);
            iv_zan=itemView.findViewById(R.id.iv_zan);
            tv_zan_num=itemView.findViewById(R.id.tv_zan_num);
        }
    }

}
